package io;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class TextDemo {
    /*
     写文本文件
     */
    public static void main(String[] args) throws IOException {
        /*
        将字符串按照“UTF-8” 标注进行编码，得到编码以后
        的字节序列：英文1字节，希腊字母2字节，中文3字节

         */

        String str = "嘉然今天吃什么";
        byte[] bytes = str.getBytes("UTF-8");
        System.out.println(bytes.length);
        FileOutputStream out = new FileOutputStream("./text.txt");
        out.write(bytes);
        out.close();

        //读取文本文件

        bytes = new byte[2000];
        FileInputStream in = new FileInputStream("./text.txt");
        int n = in.read(bytes);
        System.out.println("读取了字节数"+n);
        //对读取结果进行解码：
        String s = new String(bytes,0,n,"UTF-8");
        System.out.println(s);
        in.close();

    }
}
